package com.nstar.orderexpress.commands
{
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.commands.Command;
	import mx.controls.Alert;
	import mx.utils.ObjectUtil;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.collections.ArrayCollection;
	import com.nstar.orderexpress.model.Model;
	import com.nstar.orderexpress.business.OrderExpressDelegate;
	import com.nstar.orderexpress.control.QueryEvent;
	import com.nstar.components.ErrorView;
	import com.nstar.base.util.Logger;
	import com.nstar.orderexpress.vo.AreaVO;
	import com.nstar.orderexpress.vo.OrderMenuVO;
	import com.nstar.orderexpress.vo.Menu2RestVO;
	import com.nstar.orderexpress.vo.RestInfoVO;
	import com.nstar.orderexpress.vo.UserLevelVO;
	import com.nstar.orderexpress.vo.UserTypeVO;
	import com.nstar.orderexpress.vo.FoodTypeVO;
	import com.nstar.base.util.SystemUtils;

	public class QueryCommand implements Command, IResponder
	{
		private var model:Model = Model.getInstance();
		private var logger:Logger = Logger.getInstance();
		private var benchmark:Date;
		
		public function execute(event:CairngormEvent):void
		{ 
			//TODO: implement function
			var delegate:OrderExpressDelegate = new OrderExpressDelegate(this);//由代理来调用异步处理方法
			//强制转换成自定义的CairngormEvent事件，以获得自定义事件的属性
			var queryEvent:QueryEvent = QueryEvent(event);
			//调用代理中的方法
			benchmark = new Date();
			logger.debug("begin benchmark...");
			delegate.findMenus(queryEvent.queryVO);
		}
		
		//正常返回的处理
		public function result(data:Object):void
		{
			logger.debug("1:"+SystemUtils.calcInterval(benchmark));
			//TODO: implement function
			var ary:Array = data as Array;
			var list:ArrayCollection = new ArrayCollection();
			for(var i:int =0;i<ary.length;i++){
				//因为类经过映射，所以可以直接赋给VO对象
				var area:AreaVO = ary[i].area;
				var orderMenu:OrderMenuVO = ary[i].orderMenu;
				var restInfo:RestInfoVO = ary[i].restInfo;
				var menu2Rest:Menu2RestVO = ary[i].menu2Rest;

				var userLevel:UserLevelVO = ary[i].userLevel;
				var userType:UserTypeVO = ary[i].userType;
				var foodType:FoodTypeVO = ary[i].foodType;

				orderMenu.foodType = foodType;
				
				restInfo.userLevel = userLevel;
				restInfo.userType = userType;
				
				menu2Rest.area = area;
				menu2Rest.orderMenu = orderMenu;
				menu2Rest.restInfo = restInfo;
				
				list.addItem(menu2Rest);
			}
			logger.debug("2:"+SystemUtils.calcInterval(benchmark));
			model.mainApp.mainView.filterView.atlMenuList.dataProvider = list;
			logger.debug("3:"+SystemUtils.calcInterval(benchmark));
		}
		
		//失败返回处理
		public function fault(info:Object):void
		{
			//TODO: implement function
			ErrorView.showErrorView(info);
		}
		
		private function translate(item:Object ):Object{
			return item;
		}			
		
	}
}